//
//  RootViewController.m
//  碟片
//
//  Created by lmt on 15/12/12.
//  Copyright (c) 2015年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "RootViewController.h"
#import "LTView.h"

// 定义三个全局变量  i 控制播放暂停的变化  j 控制喜欢的变化  timer是时间方法
NSInteger i = 0;
NSInteger j = 1;
NSTimer *timer;

@interface RootViewController ()

@end

@implementation RootViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor grayColor];
    
    // title
    LTView *title = [[LTView alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 60)];
    title.backgroundColor = [UIColor blackColor];
    title.layer.borderWidth = 0.5;
    title.layer.borderColor = [UIColor whiteColor].CGColor;
    title.label1.text = @"我怀念的";
    title.label2.text = @"孙燕姿";
    [self.view addSubview:title];
    [title release];
    
    
    
    // dvd
    self.dvd = [[UIImageView alloc] initWithFrame:CGRectMake(30, 150, self.view.frame.size.width - 60, self.view.frame.size.width - 60)];
    self.dvd.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:self.dvd];
    self.dvd.layer.cornerRadius = self.dvd.frame.size.width / 2;
    self.dvd.clipsToBounds = YES;
    self.dvd.image = [UIImage imageNamed:@"syz.jpg"];
    [self.dvd release];
    
    // 喜欢按钮
    self.like = [UIButton buttonWithType:UIButtonTypeSystem];
    self.like.frame = CGRectMake(30, self.dvd.frame.origin.y + self.dvd.frame.size.height + 30, 30, 30);
    [self.like setBackgroundImage:[UIImage imageNamed:@"like.png"] forState:UIControlStateNormal];
    [self.like addTarget:self action:@selector(likeAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.like];
    
    // 随机播放按钮
    UIButton *random = [UIButton buttonWithType:UIButtonTypeSystem];
    random.frame = CGRectMake(120, self.dvd.frame.origin.y + self.dvd.frame.size.height + 30, 30, 30);
    [random setBackgroundImage:[UIImage imageNamed:@"suiji.png"] forState:UIControlStateNormal];
    [self.view addSubview:random];
    
    
    // 列表按钮
    UIButton *lie = [UIButton buttonWithType:UIButtonTypeSystem];
    lie.frame = CGRectMake(210, self.dvd.frame.origin.y + self.dvd.frame.size.height + 30, 30, 30);
    [lie setBackgroundImage:[UIImage imageNamed:@"lie.png"] forState:UIControlStateNormal];
    [self.view addSubview:lie];
    
    // 彩蛋按钮
    UIButton *caidan = [UIButton buttonWithType:UIButtonTypeSystem];
    caidan.frame = CGRectMake(300, self.dvd.frame.origin.y + self.dvd.frame.size.height + 30, 30, 30);
    [caidan setBackgroundImage:[UIImage imageNamed:@"caidan.png"] forState:UIControlStateNormal];
    [self.view addSubview:caidan];
    
    // 滑动控制器
    self.slider = [[UISlider alloc] initWithFrame:CGRectMake(30, self.like.frame.origin.y + self.like.frame.size.height +20, self.view.frame.size.width-60, 30)];
    // 滑块的最大值
    self.slider.maximumValue = 2500;
    // 是否重复
    self.slider.continuous = YES;
    [self.view addSubview:self.slider];
    [self.slider release];
    
    // 播放按钮
    self.play = [UIButton buttonWithType:UIButtonTypeCustom];
    self.play.frame = CGRectMake(150, self.slider.frame.origin.y + self.slider.frame.size.height + 10, 50, 50);
    self.play.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.play];
    [self.play setImage:[UIImage imageNamed:@"bofang.png"] forState:UIControlStateNormal];
    [self.play addTarget:self action:@selector(playAction:) forControlEvents:UIControlEventTouchUpInside];
    
    // 上一首按钮
    UIButton *up = [UIButton buttonWithType:UIButtonTypeSystem];
    up.frame = CGRectMake(80, self.slider.frame.origin.y + self.slider.frame.size.height + 20, 30, 30);
    [up setBackgroundImage:[UIImage imageNamed:@"shang.png"] forState:UIControlStateNormal];
    [self.view addSubview:up];
    
    // 下一首按钮
    UIButton *down = [UIButton buttonWithType:UIButtonTypeSystem];
    down.frame = CGRectMake(240, self.slider.frame.origin.y + self.slider.frame.size.height + 20, 30, 30);
    [down setBackgroundImage:[UIImage imageNamed:@"xia.png"] forState:UIControlStateNormal];
    [self.view addSubview:down];
    
    
    
}

- (void)playAction:(UIButton *)play
{
    i++;

    if (i % 2 == 1) {
        // 点击一次 就切换暂停和播放图案
        [self.play setImage:[UIImage imageNamed:@"zanting.png"] forState:UIControlStateNormal];
        self.slider.value = 0;
        // Timer 是上面定义的NSTimer变量 旋转
       timer =  [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(rotate) userInfo:nil repeats:YES];
    }
    
    else {
        // 点击一次 就切换暂停和播放图案
        [self.play setImage:[UIImage imageNamed:@"bofang.png"] forState:UIControlStateNormal];
        
        // 取消Timer  停止旋转
        [timer invalidate];
    }
}

-(void)likeAction
{
    j++;
    if (j % 2 == 1)
    {
        [self.like setBackgroundImage:[UIImage imageNamed:@"like.png"] forState:UIControlStateNormal];
    }
    else
    {
        [self.like setBackgroundImage:[UIImage imageNamed:@"hong.png"] forState:UIControlStateNormal];
    }
}


- (void)rotate
{
    // 旋转的方法 M_1_PI - 0.3 是角度  M_1_PI系统的
    self.dvd.transform = CGAffineTransformRotate(self.dvd.transform, M_1_PI - 0.3);
//    self.dvd.transform = CGAffineTransformMakeRotation(0.1 * self.slider.value);
    // 让滑块自动+1
    self.slider.value += i;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
